package com.cjjd.c.http;


import android.content.Context;
import android.content.Intent;

import com.cjjd.c.App;
import com.cjjd.c.EventBusKeys;
import com.cjjd.c.bean.HttpResult;
import com.cjjd.c.feature.mine.login.LoginActivity;
import com.cjjd.c.utils.LogUtil;
import com.cjjd.c.utils.NetworkUtil;
import com.cjjd.c.utils.SharePrefUtil;
import com.cjjd.c.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * Created by Administrator on 2016/12/29.
 */

public abstract class CallbackImpl<T> implements Callback<HttpResult<T>> {
    @Override
    public void onResponse(Call<HttpResult<T>> call, Response<HttpResult<T>> response) {

        if (response.isSuccess()) {
            onRequestFinish();
            if (response.body() == null) {
                ToastUtil.showToast("未知错误");
                return;
            }
            if (response.body() != null && (response.body().getReturn_code() == 5)) {
                // 微信登录，需要绑定手机号
                responseSuccess(call, response);
                return;
            }
            if (response.body() != null && response.body().getReturn_code() == 51) {
                ToastUtil.showToast("登录过期，请重新登录");
                reLogin();
                return;
            }
            if (response.body() != null && response.body().getReturn_code() != 0) {
                if(onFaulure()) {
                    ToastUtil.showToast(response.body().getMessage());
                }
                return;
            }
            if (response.body() != null && (response.body().getReturn_code() == 0)) {
                responseSuccess(call, response);
            }


        } else {
            onRequestFinish();
            ToastUtil.showToast("服务器错误或网络异常");
            return;
        }

    }

    public boolean onFaulure(){

        return  true;
    };

    private void reLogin() {
        Context mContext = App.getContext();
        SharePrefUtil.clear(mContext);
        EventBus.getDefault().post(EventBusKeys.unLogin);
        Intent intent = new Intent(mContext, LoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mContext.startActivity(intent);
    }


    @Override
    public void onFailure(Call<HttpResult<T>> call, Throwable t) {
        onRequestFinish();
        if (!NetworkUtil.isNetworkAvailable(App.getContext())) {
            ToastUtil.showToast("无网络连接，请检查网络后重试");
            return;
        }
        if (t.toString().equals("java.net.SocketTimeoutException")
                || t.toString().equals("java.net.ConnectTimeoutException") || t.toString().contains("ETIMEDOUT")) {
            ToastUtil.showToast("请求超时，请重试");
            return;
        }
        if (t.toString().contains("ConnectException")) {
            ToastUtil.showToast("网络连接错误，请检查网络。");
            return;
        }
        if (t.toString().contains("com.google.gson.JsonSyntaxException")) {
            ToastUtil.showToast("数据解析错误");
            LogUtil.e("dada", t.toString());
            return;
        }
    }

    /**
     * 请求结束(不关注是否请求成功)后执行的操作, 如： 取消刷新，  取消dialog 等等,  非必须
     */
    protected void onRequestFinish() {
    }

    /**
     * 无异常的请求成功
     *
     * @param call
     * @param response
     */
    public abstract void responseSuccess(Call<HttpResult<T>> call, Response<HttpResult<T>> response);


}
